using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using WPCoco;
using WPCoco.Graphics;
using WPCoco.Tasks;

namespace UsingSpritesDemo.Scenes
{
    public class SpriteScene : Scene
    {
        public SpriteScene()
        {
            // Creating a base sprite where add the logos, it must use the same texture
            common = new Sprite(Coco.Load<Texture2D>("Logo"), Rectangle.Empty);
            common.Anchor = Vector2.Zero;
            common.Width = Screen.Width;
            common.Height = Screen.Height;

            AddNode(common);

            // Creating an atlas for a batch draw and binding to "common" sprite
            AtlasSprite atlas = new AtlasSprite(common);

            // Creating and animation that periodically add new sprites to the scene
            RunTask(new Repeat(
                        new Sequence(
                            new DelegateTask(new Action(() => { AddSprite(); })),
                            new FiniteTime(0.1f))
                        , 10000));
        }

        Sprite common;

        Random rnd = new Random();

        void AddSprite()
        {
            // Creating starting and ending positions
            Vector2 start = new Vector2(rnd.Next(-200, -100), rnd.Next(Screen.Height)),
                      end = new Vector2(Screen.Width - start.X, start.Y);

            // Creating a sprite with the desired texture
            Sprite spr = new Sprite(Coco.Load<Texture2D>("Logo"));
            spr.Position = start;

            // Adding to common sprite that has been linked to an "AtlasSprite" for batch draw
            common.AddNode(spr);
            
            // Run a task that move the sprite to the final position and finally remove it from scene.
            float time = (end - start).Length() / rnd.Next(20, 150);
            spr.Color = Color.Lerp(Color.White, Color.Black, time / 60);
            spr.RunTask(new Sequence(
                            new MoveTo(end, time),
                            new Remove()));
        }
    }
}
